<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>权限管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">权限列表</div>
                <div class="layui-card-body">
                    <div class="layui-btn-group test-table-operate-btn" style="margin-bottom: 10px;">
                        <button class="layui-btn" data-type="addRole">添加权限</button>
                    </div>

                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate"></table>

                    <script type="text/html" id="test-table-operate-barDemo">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="addmodal" style="display: none;padding: 20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">

        <div class="layui-form-item">
            <label class="layui-form-label">*权限名称：</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" placeholder="请输入权限名称" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">对应方法：</label>
            <div class="layui-input-block">
                <input type="text" name="name"  placeholder="请输入方法名（Controller/action）" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">菜单展示：</label>
            <div class="layui-input-block">
                <input type="radio" name="ismenu" value="1" title="是">
                <input type="radio" name="ismenu" value="0" title="否">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">顶级导航icon：</label>
            <div class="layui-input-block">
                <input type="text" name="icon"  placeholder="layui的图标icon" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" lay-filter="addcat">
            <div class="layui-inline">
                <label class="layui-form-label">*上级权限</label>
                <div class="layui-input-inline">
                    <select name="pid" lay-search="" lay-verify="required">
                        <option value="">选择上级权限</option>
                        <option value="0">顶级权限</option>
                        {volist name="topmenu" id="vol"}
                            <option value='{$vol.id}'>{$vol.level|str_repeat='&emsp;',###*2}{$vol.title}</option>
                        {/volist}
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="addSubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>

</div>

<div id="editmodal" style="display: none;padding: 20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">

        <div class="layui-form-item">
            <label class="layui-form-label">*权限名称：</label>
            <div class="layui-input-block">
                <input type="text" name="title" id="editTitle" lay-verify="required" placeholder="请输入权限名称" autocomplete="off" class="layui-input">
                <input type="hidden" name="id" id="editId">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">对应方法：</label>
            <div class="layui-input-block">
                <input type="text" name="name" id="editName"  placeholder="请输入方法名（Controller/action）" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">*菜单展示：</label>
            <div class="layui-input-block" id="editIsMenu">
                <input type="radio"  name="ismenu" value="1" title="是">
                <input type="radio"  name="ismenu" value="0" title="否">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">顶级导航icon：</label>
            <div class="layui-input-block">
                <input type="text" name="icon" id="editicon"  placeholder="layui的图标icon" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" lay-filter="addcat">
            <div class="layui-inline">
                <label class="layui-form-label">*上级权限</label>
                <div class="layui-input-inline">
                    <select name="pid" id="editPid" lay-verify="required">
                        <option value="">选择上级权限</option>
                        <option value="0">顶级权限</option>
                        {volist name="topmenu" id="vol"}
                            <option value='{$vol.id}'>{$vol.level|str_repeat='&emsp;',###*2}{$vol.title}</option>
                        {/volist}
                    </select>
                </div>
            </div>
        </div>


        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="editSubmit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </form>

</div>


<script src="/static/layuiadmin/layui/layui.js"></script>

<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate','set'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,form = layui.form;

        table.render({
            elem: '#test-table-operate'
            ,url: "{:url('Auth/authList')}"
            ,cols: [[
                { align:'center',width:100,field:'id',  title: 'ID', sort: true, fixed: 'left'}
                ,{field:'title',  title: '权限名称',templet:function(d){
                        return new Array( d.level + 1 ).join('|___') + d.title;
                    }}
                ,{ align:'center',field:'name',  title: '对应方法'}
                ,{ align:'center',field:'ismenu',  title: '是否为菜单',templet:function(d){
                        if(d.ismenu == 0){
                            return '否';
                        }else{
                            return '是';
                        }
                    }}
                ,{ align:'center',field:'pid',  title: '上级id'}
                ,{ align:'center', title: '操作', fixed: 'right', toolbar: '#test-table-operate-barDemo'}
            ]]
            ,page: false
        });

        form.render(null, 'component-form-group');



        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){                            //删除操作
                layer.confirm('确认删除吗', function(index){
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: "{:url('Auth/authDel')}",
                        type: 'post',
                        dataType: 'text',
                        data: {id:data.id},
                        success: function(msg){
                            layer.close(loading);
                            if (msg === "1") {
                                layer.msg('删除成功',{icon:1,time:1000},function(){
                                    obj.del();
                                    layer.close(index);
                                });

                            }else{
                                layer.msg(msg,{icon:2,time:2000});
                            }

                        }
                    })

                });
            } else if(obj.event === 'edit'){                           //修改操作
                $('#editTitle').val(data.title);
                $('#editId').val(data.id);
                $('#editName').val(data.name);
                $('#editPid').val(data.pid);
                $('#editicon').val(data.icon);
                $("#editIsMenu input:radio[value ="+data.ismenu+"]").attr('checked','true');

                form.render();

                var editmodal = layer.open({
                    title:'修改权限'
                    ,type: 1
                    ,shadeClose: true
                    ,area: admin.screen() < 2 ? ['80%'] : ['700px']
                    ,content: $('#editmodal')
                });
            }
        });
        //添加提交
        form.on('submit(addSubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Auth/authAdd')}",
                type: 'post',
                dataType: 'text',
                data: {data:data.field},
                success: function(info){
                    if (info === "1") {
                        layer.msg('添加成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg('添加失败',{icon:2,time:1000})
                    }
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //修改提交
        form.on('submit(editSubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Auth/authEdit')}",
                type: 'post',
                dataType: 'text',
                data: {data:data.field},
                success: function(info){
                    if (info === "1") {
                        layer.msg('修改成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg('修改失败',{icon:2,time:1000})
                    }
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        var $ = layui.$, active = {
            addRole: function(){
                layer.open({
                    title:'添加角色'
                    ,type: 1
                    ,shadeClose: true
                    ,area: admin.screen() < 2 ? ['80%'] : ['700px']
                    ,content: $('#addmodal')
                });

            }
        };

        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });

</script>
</body>
</html>